package com.base.system.controller.sys;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.system.base.http.HttpResult;
import com.base.system.config.security.UserKit;
import com.base.system.jpa.entity.JpaUser;
import com.base.system.jpa.entity.JpaUserMenu;
import com.base.system.jpa.entity.JpaUserRole;
import com.base.system.mybatisPlus.entity.MybatisUser;
import com.base.system.service.MenuService;
import com.base.system.service.UserService;
import com.base.system.web.vm.UserVm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户区")
@RestController
@RequestMapping("/sys/user")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private MenuService menuService;

    @PostMapping("/getUserInfo/{token}")
    @ApiOperation(value = "通过获取用户信息")
    public HttpResult<MybatisUser> getUserInfo(@PathVariable String token) {
        return HttpResult.ok(UserKit.getUserByToken(token));
    }

    @PostMapping("/page")
    @ApiOperation("用户列表")
    @PreAuthorize("hasAuthority('user_list')")
    public HttpResult<IPage<MybatisUser>> page(Page<MybatisUser> page, @RequestBody UserVm vm) {
        return HttpResult.ok(userService.page(page, vm));
    }

    @PostMapping("/save")
    @ApiOperation("用户新增")
    @PreAuthorize("hasAuthority('user_add')")
    public HttpResult<JpaUser> add(@RequestBody JpaUser user) {
        return HttpResult.ok(userService.save(user));
    }

    @PutMapping("/update")
    @ApiOperation("用户修改")
    @PreAuthorize("hasAuthority('user_update')")
    public HttpResult<JpaUser> update(@RequestBody JpaUser user) {
        return HttpResult.ok(userService.udate(user));
    }

    @PostMapping("/setUserWithMenu")
    @ApiOperation("设置用户可见菜单项")
    @PreAuthorize("hasAuthority('user_set_menu')")
    public HttpResult<Void> setUserWithMenu(@RequestBody List<JpaUserMenu> userMenus) {
        menuService.saveUserMenu(userMenus);
        return HttpResult.ok();
    }

    @PostMapping("/setUserWithRole")
    @ApiOperation("设置用户角色")
    @PreAuthorize("hasAuthority('user_set_role')")
    public HttpResult<Void> setUserWithRole(@RequestBody List<JpaUserRole> userRoles) {
        userService.saveUserRole(userRoles);
        return HttpResult.ok();
    }

    @GetMapping("/getMenu/{userId}")
    @ApiOperation("获取所有已选项id")
    public HttpResult<List<Long>> getMenu(@PathVariable Long userId) {
        return HttpResult.ok(menuService.selectedMenuByUserId(userId));
    }

    @GetMapping("/getUserWithRole/{userId}")
    @ApiOperation("获取当前用户所有已选项角色id")
    public HttpResult<List<Long>> getUserWithRole(@PathVariable Long userId) {
        return HttpResult.ok(userService.getUserWithRole(userId));
    }
}
